/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplexml.utiles;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/*
 * @author DIANA
 */
public class Eventos {
    char caracter=' ';
    //Evento de teclado para lenguaje
    public char eventocodigo(JTextArea j) {
  
        j.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char a = e.getKeyChar();       
                System.out.println(a +" letra");
                if (a=='[') {
                 caracter='<';   
                }
            }
        });
        return caracter;
    }

    //Teclado solo letras
    public void eventoLetras(JTextArea j) {

        j.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char a = e.getKeyChar();
                if (Character.isDigit(a)) {
                    e.consume();
                }
            }
        });
    }

    public void eventoNumeros(JTextField j) {

        j.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char a = e.getKeyChar();
                if (Character.isDigit(a)) {
                } else {
                    e.consume();
                }
            }
        });

    }
    int punt = 0;
    String texto = "";

    public void eventoDecimal(JTextField j) {

        texto = j.getText();

        j.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                punt = 0;
                char a = e.getKeyChar();
                texto = String.valueOf(a).concat(texto);
                for (int i = 0; i < texto.length(); i++) {
                    if (texto.charAt(i) == '.') {
                        punt++;
                    }
                }
                texto="";
                
                if (Character.isDigit(a) || (a == '.') || a == e.VK_BACK_SPACE) {
                    if (a == '.') {
                        if (punt >= 2) {
                            e.consume();
                        }
                    }
                } else {
                    e.consume();

                }
            }
        });

    }
}
